define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'deerhome/xcxmsg/index' + location.search,
                    add_url: 'deerhome/xcxmsg/add',
                    edit_url: 'deerhome/xcxmsg/edit',
                    del_url: 'deerhome/xcxmsg/del',
                    multi_url: 'deerhome/xcxmsg/multi',
                    import_url: 'deerhome/xcxmsg/import',
                    table: 'deerhome_xcxmsg',
                }
            });

            var table = $("#table");
            table.on('post-common-search.bs.table', function (event, table) {
                $(".columns-right").hide();
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                uniqueId: 'id',
                search: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'title', title: __('消息类别'), operate: 'LIKE'},
                        {field: 'pushed', title: __('微信小程序通知'),searchList:{"1":"是","2":"否"}, formatter: Table.api.formatter.label},
                        {field: 'id', title: __('短信通知'), formatter: function (value, row, index) {
                            var html="";
                            html+='<div style="background:#ededed;border-radius:10px;width:130px;margin:0 auto;display: flex;justify-content: center;align-items: center;overflow:hidden;padding:0 10px;">';
                            html+='<a data-shade="0.5" href="deerhome/xcxmsg/edit/ids/'+row.id+'" title="【'+row.title+'】短信配置" class="btn btn-success btn-xs btn-dialog" style="margin-right:10px;"><i class="fa fa-gear"></i> 配置</a>';
                            if(row.sms_open==1){
                                html+='<a href="javascript:;" data-toggle="tooltip" class="btn-change " data-index="0" data-id="'+row.id+'" data-params="sms_open=2" data-original-title="点击关闭"><i class="fa fa-toggle-on text-success  fa-2x"></i></a>';
                            }else{
                                html+='<a href="javascript:;" data-toggle="tooltip" class="btn-change " data-index="0" data-id="'+row.id+'" data-params="sms_open=1" data-original-title="点击开启"><i class="fa fa-toggle-on text-gray fa-2x"></i></a>';
                            }
                            html+='</div>';
                            return html;
                        }},
                        
                    ]
                ]
            });

            $(".btn-pushtowx").click(function () {
                let ids = Table.api.selectedids(table);
                if (ids.length == 0) {
                    Layer.msg("请选择要推送的消息类别模板")
                    return false;
                }
                let data=[];
                for(let i=0;i<ids.length;i++){
                    let row=table.bootstrapTable('getRowByUniqueId',ids[i]);
                    data[i]={};
                    data[i].id=row.id;
                    data[i].title=row.title;
                    data[i].tid=row.tid;
                    data[i].kidList=row.keyword;
                }
                let index=0;
                let ok=0;
                let err=[];
                let push=function(){
                    if(index>=data.length){
                        table.bootstrapTable('refresh');
                        Layer.open({title:'处理完成',btn:false,content:"<b>合计推送"+data.length+"条，成功"+ok+"条，失败"+err.length+"条</b><br/>"+err.join("<br/>")});
                        return false;
                    }
                    Layer.msg("正在推送第 "+(index+1)+"/"+data.length+" 条数据");
                    Fast.api.ajax({
                            url: "deerhome/xcxmsg/push_to_wx",
                            data:data[index],
                            loading: true
                        },
                        function (res,ret) {
                            index++;
                            ok++;
                            setTimeout(push,500);
                        },
                        function (res,ret) {
                            err.push("【"+data[index].title+"】"+ret.msg);
                            index++;
                            setTimeout(push,500);
                        }
                    );
                }
                push();
            })
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
